package com.xiaowu.datajpa.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Slf4j
@Aspect
@Component
public class HttpAspect {
    @Pointcut("execution(* com.xiaowu.datajpa.api.StudentApiController.*(..))")
    public void log(){
    }
    @Before("log()")
    public void doBefore(){
        System.out.println("doBefore");
    }
    @After("log()")
    public void doAfter(){
        System.out.println("doAfter");
    }
    @Around("log()")
    public Object Around(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object obj = pjp.proceed(pjp.getArgs());
        stopWatch.stop();
        long cost = stopWatch.getTotalTimeMillis();
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        String methodName = signature.getDeclaringTypeName()+"."+signature.getName();
        log.info("执行方法：{}，耗时：{}ms",methodName,cost);
        return obj;
    }
}
